/***************************************************************************
 创建者：华磊
 开始时间：         : 2020.12.17
 copyright            : (C) 深圳市华友高科有限公司
 修改说明：（每次有修改就添加一条，带有 修改人，修改时间，修改描述）

 ***************************************************************************
 *   输出指令属性编辑视窗
*                                                                         *
 ***************************************************************************/
#include "outputinstructiondelegate.h"
#include <QDebug>

OutputInstructionDelegate::OutputInstructionDelegate(QWidget *parent):
    QObject(parent)
{
}

void OutputInstructionDelegate::initialView()
{
    outputInstructionView->outputInstruction = " ";
    outputInstructionView->outputType = 0;
    outputInstructionView->index = "1";
    outputInstructionView->selectDoFlag = 1;
    outputInstructionView->doSwitch = "ON ";
    outputInstructionView->switchType = 0;
    outputInstructionView->pulseTime = 0.0;
    outputInstructionView->rIndex = "1";
    outputInstructionView->aoValue = 1.1;
    outputInstructionView->note = "";

    //outputInstructionView->updateView();
}

void OutputInstructionDelegate::selectSwitchSlot()
{
    outputInstructionView->selectDoFlag = EM_OUTPUT_TYPE_SWITCH;

    qDebug("select switch");
}

void OutputInstructionDelegate::selectPulseSlot()
{
    outputInstructionView->selectDoFlag = EM_OUTPUT_TYPE_PULSE;

    qDebug("select pulse");
}

void OutputInstructionDelegate::selectRegisterSlot()
{
    outputInstructionView->selectDoFlag = EM_OUTPUT_TYPE_REGISTER;

    qDebug("select register");
}

void OutputInstructionDelegate::clearNoteSlot()
{
    outputInstructionView->note.clear();

    qDebug("clear note");
}

void OutputInstructionDelegate::finishEditInstructionSlot()
{
    if(0 == outputInstructionView->outputType)
    {
        outputInstructionView->outputInstruction = "DO[" + outputInstructionView->index + "]" + "=";

        if(EM_OUTPUT_TYPE_SWITCH == outputInstructionView->selectDoFlag)
        {
            if(0 == outputInstructionView->switchType)
            {
                outputInstructionView->outputInstruction += "ON";
            }
            else
            {
                outputInstructionView->outputInstruction += "OFF";
            }
        }
        else if(EM_OUTPUT_TYPE_PULSE == outputInstructionView->selectDoFlag)
        {
            outputInstructionView->outputInstruction += "PULSE ";
            if(outputInstructionView->pulseTime > 0.0)
            {
                outputInstructionView->outputInstruction += ", " + QString("%1").arg(outputInstructionView->pulseTime) + "sec";
            }
        }
        else if(EM_OUTPUT_TYPE_REGISTER == outputInstructionView->selectDoFlag)
        {
            outputInstructionView->outputInstruction += "R[" + outputInstructionView->rIndex + "]";
        }
    }
    else if(1 == outputInstructionView->outputType)
    {
        outputInstructionView->outputInstruction = "AO[" + outputInstructionView->index + "]"
                + "=" + QString("%1").arg(outputInstructionView->aoValue) ;
    }

    outputInstructionView->outputInstruction = outputInstructionView->outputInstruction + "; " + "//" + outputInstructionView->note;
}

void OutputInstructionDelegate::selectDOInstructionSlot()
{
    outputInstructionView->outputType = 0;
    qDebug() << "select DO";
    outputInstructionView->updateView();
}

void OutputInstructionDelegate::selectAOInstructionSlot()
{
    outputInstructionView->outputType = 1;
    qDebug() << "select AO";
    outputInstructionView->updateView();
}
